<?php

Class articleAction extends CommonAction{

      //文章首页
      Public function index(){
           $data = D('articleRelation')->relation(true)->getArticleByMore('','','id desc','20');
           $cate = D("articleType")->getCategory();
           $cate = self::createOptionHtml($cate);
           $this->cate = $cate;
           $this->data = $data;
           $this->display('article-list');
      }

      // 博客分类
      public function articleType(){
           $data = D("articleType")->getArticleByMore(array("pid"=>0));
           $this->data = $data;
           $this->display('articletype-list');
      }

      // 博客分类添加视图
      public function article_add_view(){
          if($_GET['id']){
              $cate = D('articleType')->getArticleByOne(array("id"=>$_GET['id']));
              $data = D("articleType")->getCategory();
              $data = $this->createOptionHtml($data,$cate['pid']);
              $this->cate = $cate;
          }else {
              $data = D("articleType")->getCategory();
              $data = $this->createOptionHtml($data);
          }
          $this->data = $data;
          $this->display("article-type");
      }

      // 博客分类添加处理
      public function article_add_do(){
           $pid = $this->_post('pid');
           $name = $this->_post('category-name');
           $mod = $this->_post('mod');
           // 判断是否为空
           if(empty($name)){
                  $this->redirect('article/article_add_view');
                  exit();
           }
           $data['pid'] = !empty($pid)?$pid:0;
           $data['cname'] = $name;
           $data['status'] = 1;
           $data['order_by'] = 0;
           $data['uid'] = _decrypt(cookie('loginId'));
           $data['createtime'] = time();
           if($mod){
               $res = D('articleType')->update(array("id"=>$mod),$data);
               $tips = "修改";
           }else {
               $res = D('articleType')->addarticle($data);
               $tips = "添加";
           }
           if($res){
                 $this->show("success",U('admin/article/articleType'),$tips.'成功');
           }else{
                 $this->show("error",U('admin/article/articleType'),$tips.'失败');
           }
      }

    /**
     * 更新排序状态
     */
    public function updataOrder(){
         $id = $this->_get('id');
         $order = $this->_get('order');
         if(empty($order)){
              $data = json_encode(array(
                  "code"=>401,
                  "msg" =>"更新失败",
              ));
              print_r($data);
              exit();
         }
         $data['order_by'] = $order;
         $res = D('articleType')->update(array("id"=>$id),$data);
         if($res){
             $data = json_encode(array(
                 "code"=>200,
                 "msg" =>"更新成功",
             ));
         }else{
             $data = json_encode(array(
                 "code"=>401,
                 "msg" =>"更新失败",
             ));
         }
        print_r($data);
    }

    /**
     * 异步返回json数据
     */
      public function getArticle(){
            $id = $this->_get('id');
            $data = D("articleType")->getArticleByMore(array("pid"=>$id));
            foreach($data as $k => $v){
                  $data[$k]['createtime'] = date("Y-m-d",$v['createtime']);
            }
            print_r(json_encode($data));
      }

    /**
     * 根据id删除文章
     */
    public function delartile(){
        $id = $this->_get('id');
        $comment = D("comment")->getDataByMore(array("pid"=>$id));
        if(!empty($comment)){
              foreach($comment as $v){
                  D("comment")->delComment($v['id']);
              }
        }
        $res = D('article')->delectByid($id);
        if($res){
            $data = json_encode(array(
                    "code"=>200,
                    "msg" =>"删除成功",
                    "op"  => "success"
            ));
        }else{
            $data = json_encode(array(
                    "code"=>400,
                    "msg" =>"删除失败",
                    "op"  => "fail"
            ));
        }
        print_r($data);
    }

    /**
     * 根据id删除类别
     */
     public function delartileType(){
          $op = $this->_get('op');
          $id = $this->_get('id');
          if($op==true){
               $res = D('articleType')->delectByid($id);
               if($res){
                   $data = json_encode(array(
                       "code"=>200,
                       "msg" =>"删除成功",
                       "op"  => "success"
                   ));
               }else{
                   $data = json_encode(array(
                       "code"=>400,
                       "msg" =>"删除失败",
                       "op"  => "fail"
                   ));
               }
               print_r($data);
               exit();
          }

          if($data = D('articleType')->getArticleByMore(array('pid'=>$id))){
              $data = json_encode(array(
                     "code"=>400,
                     "msg" =>"该分类下还有二级分类",
                     "op"  => "fall"
                ));
          }else{
              $res = D('articleType')->delectByid($id);
              if($res){
                  $data = json_encode(array(
                      "code"=>200,
                      "msg" =>"删除成功",
                      "op"  => "success"
                  ));
              }else{
                  $data = json_encode(array(
                      "code"=>400,
                      "msg" =>"删除失败",
                      "op"  => "fall"
                  ));
              }
          }
          print_r($data);
     }

    /**
     *博客发布视图
     */
    public function art_add_view(){
        if($this->_get('id')){
            $id = $this->_get('id');
            $viewData = D("article")->getArticleByOne(array("id"=>$id));
            $this->mod = 1;
            $this->viewdata = $viewData;
        }
        $data = D("articleType")->getCategory();
        $data = $this->createOptionHtml($data);
        $this->data = $data;
        $this->display('article-add');
    }

    /**
     *博客发布处理
     */
    public function art_add_do(){
        $data['title'] = $this->_Post('title');
        $data['Mintitle'] = $this->_Post('min-title');
        $data['sort_id'] = $this->_Post("order_by");
        $data['cid'] = $this->_Post('cid');
        $data['key_word'] = $this->_Post("key_word");
        $data['uid'] = cookie('loginId');
        $data['view'] = 0;
        $data['comment'] = 0;
        $data['content'] = $this->_post('content');
        $data['time'] = time();
        // 没有写摘要自动生成摘要
        if(!$this->_post('art')){
                $conten = strip_tags($data['content']); //先去除html 标签
                $data['abstract'] = msubstr($conten,0,40);
        }else{
                $data['abstract'] = $this->_Post('art');
        }
        $FILE1 = $_FILES['file_pic'];
        $FILE2 = $_FILES['file_name'];
        if($FILE1['error']!=4){
              // 封面上传的地址
              $cover_path = "Data/Upload/images/";
              $res = $this->_uploadImage($FILE1,$cover_path,800,800);
              if($res['code']==200) {
                  $data['cover'] = $res['savepath'];
              }
        }else{
            $data['cover'] = "Data/Upload/images/1421841277109.jpg";
        }
        if($FILE2['error']!=4){
            // 附件上传的地址
            $cover_path = "Data/Upload/file/";
            $res = $this->_uploadfile($FILE2,$cover_path);
            if($res['code']==200) {
                $data['atta_path'] = $res['savepath'];
            }
        }
        if($_POST['id']){
            $id = $_POST['id'];
            $res = D('article')->updata(array("id"=>$id),$data);
            $str = '修改';
        }else {
            $res = D('article')->add($data);
            $str = '发布';
        }
        if($res){
            $this->show('success',U('admin/article/Index'),$str.'成功');
        }else{
            $this->error('success',U('admin/article/Index'),$str.'失败');
        }
    }
}